// 该文件用于创建整个应用的路由器
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Welcome from '../components/Welcome.vue'
import Users from '../components/Users/Users.vue'
import Rights from '../components/power/Rights.vue'
import Roles from '../components/power/Roles.vue'
import Cate from '../components/goods/Cate.vue'
import Params from '../components/goods/Params.vue'
import GoodsList from '../components/goods/List.vue'
import Add from '../components/goods/Add.vue'
import Order from '../components/order/Order.vue'
import Report from '../components/report/report.vue'

const router = new VueRouter({
    routes: [
        // 路由重定向
        { path: '/', redirect: '/login' },
        {
            path: '/login',
            component: Login
        },
        {
            path: '/home',
            component: Home,
            meta: { isAuth: true, title: '东平大学', },
            redirect: '/Welcome',
            children: [
                // 欢迎界面
                {path: '/Welcome',component: Welcome},
                //用户管理界面
                {path: '/users', component: Users},
                // 用户权限界面
                {path:'/rights', component: Rights},
                // 角色列表
                {path:'/roles',component:Roles},
                // 商品分类路由
                {path:'/categories',component:Cate},
                // 分类参数路由
                {path:'/params',component:Params},
                // 商品列表路由
                {path:'/goods',component:GoodsList},
                // 添加商品页面
                {path:'/goods/add', component:Add},
                // 订单管理
                {path:'/orders',component:Order},
                // 数据报表
                {path:'/reports',component:Report}
            ]
        }
    ]
})

// 前置路由守卫
router.beforeEach((to, from, next) => {
    // 如果访问的这个路由需要守卫就进入if里面 如果不需要守卫就直接放行
    if (to.meta.isAuth) {
        if (sessionStorage.getItem('token')) {
            next()
        } else {
            alert('您无权查看')
            next('/login')
        }
    } else {
        next()
    }
})

// 后置路由守卫
router.afterEach((to) => {
    document.title = to.meta.title || '后台管理系统'
    // console.log('@@@', to, from);
})

export default router